package com.aote.model.block;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLTableModel;


/**
 * 显示实体的制定属性块模版
 */
public class EntitiesBlockModel implements HTMLTableModel {

	/**
	 * 组件
	 */
	protected HTMLBasicComponent component = null;

	/**
	 * 配置的实体属性名
	 */
	private String entityAttrName = "";

	/**
	 * 数据，List的每一项为参数值对象
	 */
	protected List datas = new LinkedList();

	public void init(HttpServletRequest request) throws Exception {

	}

	public void setComponent(HTMLBasicComponent component) {
		this.component = component;
	}

	public int getColumnCount() {
		return this.datas.size();
	}

	public String getColumnName(int index) {
		Entity entity = (Entity) this.datas.get(index);
		String result = (String) entity.getAttributes()
				.get(this.entityAttrName);
		return result;
	}

	public int getRowCount() {
		return 0;
	}

	public void setCondition(String condition) {
	}

	public void modifyCellValue(Element td, Document doc, int row, int col) {
	}

	public String getType(int col) {
		return null;
	}

	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 得到配置的实体类型，实体显示的属性名
		String entityType = this.component.getElement().getAttribute(
				"entitytype");
		this.datas = this.getEntities(entityType);
		this.entityAttrName = this.component.getElement().getAttribute(
		"showattr");
	}

	/**
	 * 得到实体
	 */
	private List getEntities(String entityType) {
		try {
			Hashtable ht = new Hashtable();
			ht.put("entitytype", entityType);
			List result = EntityManager.getInstance().getObjects(
					"com.browsesoft.baseadapter.SimpleEntity", ht);
			return result;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
